package com.yuanduo_app.widget

import android.animation.ValueAnimator
import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.util.AttributeSet
import android.view.View
import android.widget.ViewAnimator

/**
 * versions:
 * coding by pmj on 2021/7/22
 * desc: 相机拍照的按钮
 */
@SuppressLint("WrongConstant")
class CameraBtnView @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : View(context, attrs, defStyleAttr) {


    val paint: Paint by lazy {
        Paint().apply {
            isAntiAlias = true
            style = Paint.Style.FILL
        }
    }


    var process =0

    val animator:ValueAnimator by lazy {
        ValueAnimator.ofArgb(0,80,0).apply {
            duration = 1500
            repeatCount = ValueAnimator.INFINITE
            repeatMode = ValueAnimator.INFINITE
            addUpdateListener {
                process = it.animatedValue as Int
                postInvalidate()
            }
        }

    }


    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)

        paint.setColor(Color.argb(process,255,255,255))
        canvas?.drawCircle(width/2f,height/2f,width/2f,paint)
        paint.setColor(Color.parseColor("#FFFFFF"))
        canvas?.drawCircle(width/2f,height/2f,width*0.75f/2,paint)
    }


    fun startAnimator(){
        animator.start()
    }


    fun stopAnimator(){
        animator.pause()
    }


}